package com.yeheng.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.google.protobuf.Internal;
import com.yeheng.converts.MyConvert;
import com.yeheng.inteceptor.MyInterceptor;
import org.hibernate.validator.HibernateValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.format.support.FormattingConversionServiceFactoryBean;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.io.*;
import java.util.*;

/**
 * @PackgeName: com.yeheng.config
 * @ClassName: WebConfig
 * @Author: YH
 * Date: 2020/12/31 23:46
 * project name: homework1
 * @Version:
 * @Description:
 */
@Configuration
@ComponentScan("com.yeheng.controller")
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter{



    @Override//默认servlet
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override//消息转换器
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        List<MediaType> list = new ArrayList<>();
        list.add(MediaType.APPLICATION_JSON);
        converter.setSupportedMediaTypes(list);
        converter.setFeatures(SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullNumberAsZero);
        converters.add(converter);
    }
    @Bean//类型转换器
    public FormattingConversionServiceFactoryBean getConvert(){
        FormattingConversionServiceFactoryBean fcf = new FormattingConversionServiceFactoryBean();
        Set<MyConvert> set = new HashSet<>();
        set.add(new MyConvert());
        fcf.setConverters(set);
        return fcf;
    }

    //配置拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration interceptor = registry.addInterceptor(new MyInterceptor());
        interceptor.addPathPatterns("/**");
        //配置拦截路径
    }

    @Bean//校验器的消息源
    public ReloadableResourceBundleMessageSource messageSource(){
        ReloadableResourceBundleMessageSource source = new ReloadableResourceBundleMessageSource();
        source.setBasename("ValidationMessage");
        return source;
    }


    @Bean//校验器
    public LocalValidatorFactoryBean validatorFactoryBean(ReloadableResourceBundleMessageSource source){
        LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
        bean.setProviderClass(HibernateValidator.class);
        bean.setValidationMessageSource(source);
        return bean;
    }


    @Bean//视图解析器
    public InternalResourceViewResolver getView(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/");
        resolver.setSuffix(".html");

        return resolver;
    }
}
